/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package adsreis.com.Controls;

import adsreis.com.Beans.OSdetBean;

import adsreis.com.acessobd.AcessoMySql;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Pauroenriki
 */
public class OSdetControl {
    PreparedStatement pstm;
    ResultSet rs;
    String cadastraItem = "INSERT INTO OSDET(IDOS,IDPRODUTO, QTDOS, VLRVENDA) VALUES(?,?,?,?)";
    String buscaUltimoCodigo = "SELECT MAX(CODOS) AS CODOS FROM OS";
    String deleta = "DELETE FROM OSDET WHERE IDOS = ?";
        String alteraosdet = "UPDATE OSDET SET IDPRODUTO =?, QTDOS = ? WHERE IDOS =?";
    String deletaitem = "DELETE FROM OSDET WHERE IDOS = ? AND IDPRODUTO =?";
    public OSdetControl(){
        
    }
    
    public void excluirItemDET(int idorcamento, String prod){
        try{
            AcessoMySql mysql = new AcessoMySql();
           pstm = mysql.conectar().prepareStatement(deletaitem); 
           pstm.setInt(1, idorcamento);
           pstm.setString(2, prod);
           pstm.executeUpdate();
           mysql.desconectar();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void excluirOSDET(int idorcamento){
        try{
            AcessoMySql mysql = new AcessoMySql();
           pstm = mysql.conectar().prepareStatement(deleta); 
           pstm.setInt(1, idorcamento);
           pstm.executeUpdate();
           mysql.desconectar();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    
    public int buscarCodigoUltimaOS(){
        try{
            AcessoMySql mysql = new AcessoMySql();
            pstm = mysql.conectar().prepareStatement(buscaUltimoCodigo);
            rs = pstm.executeQuery();
            if (rs.last()){
                return rs.getInt("codos");                
            }
            mysql.desconectar();
        }catch (SQLException ex){
            ex.printStackTrace();
        }
        return 0;
    }
    
     public void cadastrarItem(OSdetBean osdet){
        try{
            AcessoMySql mysql = new AcessoMySql();
            pstm = mysql.conectar().prepareStatement(cadastraItem);
            pstm.setInt(1, osdet.getIDOs());
            pstm.setString(2, osdet.getIdproduto());            
            pstm.setInt(3, osdet.getQtdOs());
            pstm.setDouble(4, osdet.getPreco());
            pstm.executeUpdate();
            mysql.desconectar();
        }catch(SQLException ex){
            ex.printStackTrace();
        }
    }
    
      public void alterarOSDET(OSdetBean os){
        try{
            AcessoMySql mysql = new AcessoMySql();
           pstm = mysql.conectar().prepareStatement(alteraosdet);
           pstm.setString(1, os.getIdproduto());
           pstm.setInt(2,os.getQtdOs());
           pstm.setInt(3,os.getIDOs());
    
        pstm.executeUpdate();
           mysql.desconectar();           
        }catch (Exception e){
            e.printStackTrace();
    }
    }
    
}
